package com.shujia;

import java.sql.*;

public class HiveJDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 加载驱动
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        // 创建连接
        Connection conn = DriverManager.getConnection("jdbc:hive2://master:10000/day01","root","");
        // 创建statement
        // 防止SQL注入可以使用prepareStatement
        String clazz = "文科一班";
        PreparedStatement ps = conn.prepareStatement("select id,name,age,clazz,row_number() over(partition by clazz order by age) as rk from students where clazz = ?");
        ps.setString(1, clazz);
        //  获取结果
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            String clazz1 = rs.getString("clazz");
            System.out.println(id + "," + name + "," + age + "," + clazz1);
        }

        // 关闭连接
        rs.close();
        ps.close();
        conn.close();
    }
}
